package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.Fans;
import com.xindaoapp.happypet.bean.Fans.Fan;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 我的关注
 * 
 * @author xindaoapp
 * 
 */
public class MyAttentionAdapter extends XinDaoBaseAdapter<Fan> {
    private final String fid;

    public MyAttentionAdapter(Context context, List<Fan> iniData, int pageSize, int res,
            int loadingRes, String fid) {
        super(context, iniData, pageSize, res, loadingRes);
        this.fid = fid;
    }

    @Override
    public void nextPage(int start, int size, final ILoadNextPageData<Fan> iLoadNextPageData) {
        super.nextPage(start, size, iLoadNextPageData);
        HappyPetApplication.get().getMoccaApi()
                .getFollowList(UserState.getUserUid(), start, size, fid, new IRequest<Fans>() {

                    @Override
                    public void request(Fans t) {
                        iLoadNextPageData.loadNextPageData(t == null ? null : t.array);
                    }
                });
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent, final Fan value) {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.face = (ImageView) convertView.findViewById(R.id.face);
            holder.iv_guanzhu = (ImageView) convertView.findViewById(R.id.iv_guanzhu);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        }
        ImageLoader.getInstance().displayImage(value.userface, holder.face);
        holder.name.setText(value.fusername);

        if (TextUtils.isEmpty(fid)) {
            if (value.mutual.equals("0")) {
                holder.iv_guanzhu.setImageDrawable(mContext.getResources().getDrawable(
                        R.drawable.guanzhu_ok));
            }
            if (value.mutual.equals("1")) {
                holder.iv_guanzhu.setImageDrawable(mContext.getResources().getDrawable(
                        R.drawable.a_b));
            }
            holder.iv_guanzhu.setVisibility(View.VISIBLE);
        } else {
            holder.iv_guanzhu.setVisibility(View.GONE);
        }

        holder.iv_guanzhu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(mContext).setMessage("取消关注")
                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                deleteGuanZhu(value.followuid, position);
                                dialog.dismiss();
                            }
                        }).setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {}
                        }).create().show();
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView face;
        ImageView iv_guanzhu;
        TextView name;
    }

    private void deleteGuanZhu(String uid2, final int positon) {
        RequestParams params = new RequestParams();
        params.put("follow_uid", uid2);
        AddClient.get(Address.GUZNZHUDELETE, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, String content) {
                try {
                    getResult().remove(positon);
                    notifyDataSetChanged();
                    Toast.makeText(mContext, "取消关注成功", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.onSuccess(statusCode, content);
            }

            @Override
            public void onFailure(Throwable error, String content) {
                super.onFailure(error, content);
            }
        });
    }
}
